package com.graphics;
/**
* @author Dror
*
* email: gumjum.o.o@gmail.com
*
*/
import java.awt.Color;
import java.awt.Graphics;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.math.Quat;
import com.math.Vector;
public class Oval extends Shape {
Vector [][] boards;
public int half;
static final int PERSITION = 10;
public Oval(Camera cam,Vector dim,Vector pos,Quat rot, Color color) {
super();
this.cam = cam;
this.def_color = color;
d = new Vector(dim);
points_num = PERSITION*2;
points = new Vector[points_num];
half = points_num/2;
this.p = new Vector(pos);
this.r = new Quat(rot);
guf = new Vector[half][4];
boards = new Vector[2][half];
x = new int[half];
y = new int[half];
build();
rebuild();
}
public Oval(Camera cam, Vector dim, Color color) {
this(cam,dim,new Vector(),new Quat(),color);
}
@Override
public void build() {
for(int i = 0;i<points_num;i++)
points[i] = new Vector();
bridg();
}
public void bridg(){
for(int i = 0;i<half;i++){
guf[i][0] = points[i];
guf[i][1] = points[(i+1)%half];
guf[i][3] = points[i+half];
guf[i][2] = points[(i+1)%half+half];
}
for(int i = 0;i<half;i++){
boards[0][i] = points[i];
boards[1][i] = points[half+i];
}
}
@Override
public void rebuild() {
float sx = -d.x,sy;
float step = 2*d.x / (half/2 - 1);
float asq = d.x*d.x,b = d.y;
for(int i = 0;i<half/2;i++){
sy =(float)(b*Math.sqrt(1-(sx*sx-0.001)/(asq)));
if(i==0 || i == half/2){
points[i] .set(sx, sy,+d.z);
points[half-i-1] .set(sx, sy,+d.z);
points[i+half] .set(sx, sy,-d.z);
points[half-i-1+ half].set(sx, sy,-d.z);
}else{
points[i] .set(sx, sy,+d.z);
points[half-i-1] .set(sx,-sy,+d.z);
points[i+half] .set(sx, sy,-d.z);
points[half-i-1+ half].set(sx,-sy,-d.z);
}
sx += step;
}
for(int i=0;i<points_num;i++){
points[i].transformation(r,p);
points[i].reverseTransformation(cam.r, cam.p);
}
tmp.reverseTransformation(cam.r, cam.p,p);
compare_z = (int)(tmp.length());
}
@Override
public void show(Graphics g) {
calcColor();
boolean check;
for(int i = 0;i<2;i++){
check = true;
for(int j = 0;j<half;j++){
if(boards[i][j].z < 0)
check = false;
}
if(check){
this.convert(boards[i],x,y);
if(Shape.check(x[0], y[0], x[1], y[1], x[2], y[2]) || i == 0){
g.setColor(color);
if(this.only_lines)
g.drawPolygon(x, y, half);
else
g.fillPolygon(x, y, half);
}
}
}
for(int i = 0;i<half;i++){
if((guf[i][0].z > 0)&&(guf[i][1].z > 0)&&(guf[i][2].z > 0)&&(guf[i][3].z > 0)){
this.convert(guf[i],x,y);
if(Shape.check(x[0], y[0], x[1], y[1], x[2], y[2])){
g.setColor(this.getColor(i));
if(this.only_lines)
g.drawPolygon(x, y, 4);
else
g.fillPolygon(x, y, 4);
}
}
}
}
public Color getColor(int i){
float t = 20;
float f = ((i%half/2)+t)/t;
int rr = (int) (color.getRed()*f);
int gg = (int) (color.getGreen()*f);
int bb = (int) (color.getBlue()*f);
if(rr > 255)
rr = 255;
if(gg > 255)
gg = 255;
if(bb > 255)
bb = 255;
return new Color(rr,gg,bb);
}
public Element toXML(Document d){
Element position = d.createElement("Position");
position.setTextContent(this.p.toString());
Element rotation = d.createElement("Rotation");
rotation.setTextContent(this.r.toString());
Element dimmention = d.createElement("Dimmention");
dimmention.setTextContent(this.d.toString());
Element colo = d.createElement("Color");
colo.setTextContent(""+this.def_color.getRGB());
Element oval = d.createElement("Oval");
oval.appendChild(position );
oval.appendChild(rotation );
oval.appendChild(dimmention );
oval.appendChild(colo );
return oval;
}
public static Oval fromXML(Element e, Camera cam){
Color color = new Color(Integer.parseInt(e.getElementsByTagName("Color").item(0).getTextContent()));
Vector d = Vector.parseVector(e.getElementsByTagName("Dimmention").item(0).getTextContent());
Vector p = Vector.parseVector(e.getElementsByTagName("Position").item(0).getTextContent());
Quat r = Quat.parseQuat(e.getElementsByTagName("Rotation").item(0).getTextContent());
return new Oval(cam,d,p,r,color);
}
}